import {
    useDialogStore
} from '@/common/stores/dialog.store' ;

export default class{

    #dialog ;

    #dialogProps ;

    #applyActions ;

    #cancelActions ;

    #params ;

    constructor({
        dialog,
        apply,
        cancel,
        params,
        ...props
    } , {
        context,
        ActionProcessor,
        Expression
    }){

        const me = this ;

        me.#dialog = dialog ;

        me.#dialogProps = props ;

        if(apply){

            me.#applyActions = new ActionProcessor(apply , context);
        }

        if(cancel){

            me.#cancelActions = new ActionProcessor(cancel , context) ;
        }

        if(params){

            me.#params = new Expression(params) ;
        }
    }

    execute(params , vars){

        const 
            me = this,
            applyActions = me.#applyActions,
            cancelActions = me.#cancelActions;

        let paramsValues = me.#params ;

        if(paramsValues){

            paramsValues = paramsValues.compute(params , vars) ;
        }

        useDialogStore().show({
            params:paramsValues,
            apply(applyParams){

                if(applyActions){

                    applyActions.execute({
                        ...params,
                        params:[
                            applyParams
                        ]
                    } , {
                        ...vars
                    }) ;
                }

            },
            cancel(){

                if(cancelActions){

                    cancelActions.execute(params , {
                        ...vars
                    }) ;
                }

            },
            dialog:me.#dialog,
            ...me.#dialogProps
        }) ;

        return {
            next:false
        }
    }
}